package com.aurora.source.center.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.aurora.source.R;
import com.aurora.source.adapter.AddImgAdapter;
import com.aurora.source.base.BasePresenter;
import com.aurora.source.base.BaseWhiteToolbarActivity;
import com.aurora.source.center.presenter.AddFlowPresenter;
import com.aurora.source.center.view.IAddFlowView;
import com.aurora.source.util.DpPxUtil;
import com.aurora.source.util.SPUtils;
import com.aurora.source.widget.ItemDivider;
import com.aurora.source.widget.RecordDialogEditSureCancel;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author: ww
 * @description:流程记录-记录添加
 * @date: 2018/9/7
 */
public class AddFlowRecoredActivity extends BaseWhiteToolbarActivity implements IAddFlowView{
    @BindView(R.id.rv_add_img)
    RecyclerView rvAddImg;
    AddImgAdapter addImgAdapter;
    @BindView(R.id.et_title)
    EditText etTitle;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.et_person)
    EditText etPerson;
    @BindView(R.id.bt_add_recored)
    AppCompatButton btAddRecored;
    private String processId;
    private String parentId;
    private String batchNo;
    AddFlowPresenter addFlowPresenter;
    private int maxPic = 5;

    @Override
    public BasePresenter attach() {
        addFlowPresenter = new AddFlowPresenter(this);
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_add_flow_recored;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        processId = bundle.getString("processId");
        parentId = bundle.getString("parentId");
        batchNo = bundle.getString("batchNo");
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        simpleToolbar.setMainTitle("流程记录-记录添加");
        simpleToolbar.setLeftVisibility(View.VISIBLE);
        addImgAdapter = new AddImgAdapter(this);
        rvAddImg.setAdapter(addImgAdapter);
        rvAddImg.setLayoutManager(new GridLayoutManager(this, 4));
        rvAddImg.addItemDecoration(new ItemDivider().setDividerWith(DpPxUtil.dp2px(this, 2)));
        addImgAdapter.setOnAddImgClickListener(new AddImgAdapter.OnAddImgClickListener() {
            @Override
            public void onAddImgClick() {
                if(maxPic-addImgAdapter.getFiles().size()==0){//够了
                    showToast("最多选择5张图片");
                    return;
                }
                showPictureSelector(maxPic-addImgAdapter.getFiles().size(), true);

            }
        });
    }

    @Override
    public void doBusiness() {
    }



    @OnClick(R.id.bt_add_recored)
    public void onViewClicked() {

        if(checkNull()){
            return;
        }


        final RecordDialogEditSureCancel recordDialogEditSureCancel = new RecordDialogEditSureCancel(activity);
        recordDialogEditSureCancel.setOnSureClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recordDialogEditSureCancel.dismiss();
                Map<String, String> params2 = new HashMap<>();
                params2.put("userId", (String) SPUtils.get(activity, SPUtils.USER_ID, ""));
                params2.put("processId", processId);
                params2.put("parentId", parentId);
                params2.put("batchNo", batchNo);
                params2.put("isMain", "N");
                params2.put("processRecordTitle", etTitle.getText().toString());
                params2.put("processRecordContent", etContent.getText().toString());
                params2.put("createUserName", etPerson.getText().toString());
                addFlowPresenter.addFlowRecored(params2,addImgAdapter.getFiles());
            }
        });
        recordDialogEditSureCancel.show();

    }


    private boolean checkNull() {
        if(TextUtils.isEmpty(processId)){
            showToast("流程id为空");
            return true;
        }

        if(TextUtils.isEmpty(parentId)){
            showToast("父级节点为空");
            return true;
        }

        if(TextUtils.isEmpty(batchNo)){
            showToast("批次号为空");
            return true;
        }

        if(TextUtils.isEmpty(etTitle.getText().toString())){
            showToast("标题为空");
            return true;
        }
        if(TextUtils.isEmpty(etContent.getText().toString())){
            showToast("内容为空");
            return true;
        }
        if(TextUtils.isEmpty(etPerson.getText().toString())){
            showToast("操作人为空");
            return true;
        }
        return false;
    }

    @Override
    public void onAddFlowRecoredSuccess(String msg) {
        showToast(msg);
        hideSoftInput();
        finish();
    }

    @Override
    public void onAddFlowRecoredFail(String msg) {
        showToast(msg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureConfig.CHOOSE_REQUEST) {
            if (resultCode == RESULT_OK) {
                List<LocalMedia> localMedia = PictureSelector.obtainMultipleResult(data);
                for (LocalMedia e : localMedia) {
                    addImgAdapter.addItem(new File(e.getCutPath()));
                }
            }
        }
    }
}
